Provides a set of "lightweight" (all-JavaTM language) components that, to the maximum degree possible, work the same on all platforms. The Swing architecture provides a number of advantages in the areas of:
In general, the class names that start with "J" are the components you add to an application. Examples: JButton, JLabel, JList, JPanel, JTable, JTree. The remaining files in the swing package contain the utility classes and interfaces that the components use to function.
Instead of being restricted to a single "look and feel", you can select a look and feel and "plug
it in". An interface made of Swing components can look like a Win32 app, a Motif app, or a Mac app. It can
use the new "Metal" look and feel. Developers can use standard Swing components and design their own
look and feel (L&F) for them, and even allow users to select the look and feel they prefer. The plaf
package includes the standard "Pluggable Look And Feel" classes.
The Model-View-Controller (MVC) architecture is used consistently throughout the Swing component set.
The View and Controller parts of the architecture are combined in the component. Each component has an associated
Model class and an interface it uses. You can provide your own data-model for a
component by subclassing the Model class or by implementing the appropriate interface. For example,
you could subclass DefaultListModel
or implement the ListModel
interface, and then use
the JList setModel
method to attach your data-model to the component.
The JComponent architecture makes it easy to handle keyboard events in nested components. You register interest in a particular combination of keystrokes by creating a KeyStroke object and registering it with the component. When you register the keystroke combination and its associated action, you also specify one of the following conditions to determine when the action is initiated:
Action-interface objects provide a single point of control for program actions. For example, a toolbar icon and a menu item can reference the same Action object. When the Action object is disabled, the GUI items that reference it are automatically disabled. The Action interface extends ActionListener, specifying an enabled property as well as properties for text-descriptions and graphic icons.
As the keystroke handling shows, Swing was designed to manage nested containers gracefully. The main "heavyweight" containers (JWindow, JFrame, JDialog, and JApplet) as well as the major "lightweight" containers (JInternalFrame and JComponent) all delegate their operations to a JRootPane. This commonality produces a high degree of regularity in container nesting. In particular, since the fundamental component class (JComponent) contains a JRootPane, virtually any component can be nested within another. It means, for example, that a graphic can be nested in a list, and a combo box can be nested in a toolbar. The JRootPane class uses a JLayeredPane to manage a content pane and an optional menu bar in a way that is virtually transparent to the developer. It also provides for a glass pane -- a single pane that can overlap multiple containers and be used for drawing or to intercept mouse actions.
The JDesktopPane and JInternalFrame classes can be used to create a virtual desktop, or "multiple document interface". A JInternalFrame can be specified as iconizable, exandable, or closable, while the JDesktopPane provides real estate for them to operate in.
Insets (the space between the edges of the component and the area it is drawn in) can be specified with a blank border. In addition, many border styles are available, which can be combined to create compound borders.
The JOptionPane class provides a variety of static methods that you can invoke to create and display both message dialogs and user-choice dialogs in a variety of formats. The "message" displayed in the dialog can be a string, a string-generating object, or an arbitrary component. You can also replace the choice-buttons with components you specify for user-selections.
For example, the following line creates a simple message dialog that requires the user's confirmation:
JOptionPane.showMessageDialog(null, "The file will be deleted.");
Additional options for customized dialogs are described in the JOptionPane class.
Standard dialogs currently available include:
The JTable class provides a data-aware matrix. JTree provides hierarchical-structuring of data elements.
In addition to single-font text fields and text areas, Swing provides a JPassword field for hidden input and a JTextPane class for displaying multi-font text. In addition, the JEditorPane class provides editing capabilities for multi-font text, while the text.html and text.rtf packages handle text encoded in HyperText Markup Language (HTML) or Rich Text Format (RTF).
The undo package provides generic undo capabilities that can be used in a variety of situations.
Swing has built-in support for developers to make products that are compatible with Assistive Technologies (for alternative interfaces like, for example, braille.) All of the Swing components implement interface Accessible.
For overviews, tutorials, examples, guides, and other documentation, please see: